package com.xyazm.order.statemachine.action;

import com.xyazm.common.enums.RefundStatusEnum;
import com.xyazm.common.redis.RedisLock;
import com.xyazm.common.utils.RandomUtil;
import com.xyazm.order.dao.*;
import com.xyazm.order.domain.dto.AfterSaleStateMachineDTO;
import com.xyazm.order.domain.entity.AfterSaleRefundDO;
import com.xyazm.order.domain.entity.OrderPaymentDetailDO;
import com.xyazm.order.enums.AccountTypeEnum;
import com.xyazm.order.enums.AfterSaleStateChangeEnum;
import com.xyazm.order.factory.AfterSaleOperateLogFactory;
import com.xyazm.order.manager.IOrderNoManager;
import com.xyazm.order.remote.MarketRemote;
import com.xyazm.order.remote.PayRemote;
import com.xyazm.order.service.IAfterSaleService;
import com.xyazm.order.service.IMqMessageService;
import com.xyazm.order.statemachine.StateMachineFactory;
import com.xyazm.process.engine.model.ProcessContextFactory;
import org.springframework.beans.factory.annotation.Autowired;


/**
 * 售后状态 action
 * @author xuyou
 * @date 2023年02月25日 11:43
 */
public abstract class AfterSaleStateAction<T> extends AbstractStateAction<T, AfterSaleStateMachineDTO, AfterSaleStateChangeEnum> {

    @Autowired
    public RedisLock redisLock;
    @Autowired
    public AfterSaleLogDAO afterSaleLogDAO;

    @Autowired
    public AfterSaleInfoDAO afterSaleInfoDAO;

    @Autowired
    public AfterSaleItemDAO afterSaleItemDAO;

    @Autowired
    public OrderPaymentDetailDAO orderPaymentDetailDAO;

    @Autowired
    public AfterSaleRefundDAO afterSaleRefundDAO;

    @Autowired
    public AfterSaleOperateLogFactory afterSaleOperateLogFactory;

    @Autowired
    public OrderInfoDAO orderInfoDAO;

    @Autowired
    public StateMachineFactory stateMachineFactory;

    @Autowired
    public IAfterSaleService afterSaleService;

    @Autowired
    public IOrderNoManager orderNoManager;

    @Autowired
    public OrderAmountDAO orderAmountDAO;
    @Autowired

    public OrderItemDAO orderItemDAO;

    @Autowired
    public PayRemote payRemote;

    @Autowired
    public IMqMessageService mqMessageService;


    @Autowired(required = false)
    public ProcessContextFactory processContextFactory;

    /**
     * 获取售后退款信息
     * @param orderId     订单id
     * @param afterSaleId 售后id
     * @return
     */
    public AfterSaleRefundDO buildAfterSaleRefundDO(String orderId, String afterSaleId) {
        AfterSaleRefundDO afterSaleRefundDO = new AfterSaleRefundDO();
        afterSaleRefundDO.setAfterSaleId(afterSaleId);
        afterSaleRefundDO.setOrderId(orderId);
        afterSaleRefundDO.setAccountType(AccountTypeEnum.THIRD.getCode());
        afterSaleRefundDO.setRefundStatus(RefundStatusEnum.UN_REFUND.getCode());
        afterSaleRefundDO.setRemark(RefundStatusEnum.UN_REFUND.getMsg());
        afterSaleRefundDO.setAfterSaleBatchNo(orderId + RandomUtil.genRandomNumber(10));
        OrderPaymentDetailDO paymentDetail = orderPaymentDetailDAO.getPaymentDetailByOrderId(orderId);
        if (paymentDetail != null) {
            afterSaleRefundDO.setOutTradeNo(paymentDetail.getOutTradeNo());
            afterSaleRefundDO.setPayType(paymentDetail.getPayType());
        }
        return afterSaleRefundDO;
    }

}
